struct Solution {}

impl Solution {
    pub fn sorted_squares(a: Vec<i32>) -> Vec<i32> {
        let mut start: usize = 0;
        let mut end: usize = a.len();
        let mut b: Vec<i32> = vec![];
        while start < end {
            if a[start].abs() >= a[end - 1].abs() {
                b.push(a[start].abs() * a[start].abs());
                start += 1;
            } else {
                b.push(a[end - 1].abs() * a[end - 1].abs());
                end -= 1;
            }
        }
        b.reverse();
        b
    }
}

#[cfg(test)]
mod tests {
    use crate::Solution;

    #[test]
    fn it_works() {
        let cases = vec![
            (vec![-4, -1, 0, 3, 10], vec![0, 1, 9, 16, 100]),
            (vec![-7, -3, 2, 3, 11], vec![4, 9, 9, 49, 121]),
        ];
        for (input, expected) in cases {
            assert_eq!(Solution::sorted_squares(input), expected);
        }
    }
}
